﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace DeepZoomBookControl
{
    public class DPCancelationToken
    {
        private static Dictionary<DependencyObject, Dictionary<DependencyProperty, DPCancelationToken>> tokens;

        static DPCancelationToken()
        {
            tokens = new Dictionary<DependencyObject, Dictionary<DependencyProperty, DPCancelationToken>>();
        }

        public static DPCancelationToken GetCancelationToken(DependencyObject target, DependencyProperty property)
        {
            if (!tokens.ContainsKey(target))
                tokens.Add(target, new Dictionary<DependencyProperty, DPCancelationToken>());

            var targetTokens = tokens[target];

            if (!targetTokens.ContainsKey(property))
                targetTokens.Add(property, new DPCancelationToken(target, property));

            return targetTokens[property];
        }

        private bool isCancelling;
        private DependencyObject target;
        private DependencyProperty property;

        private DPCancelationToken(DependencyObject target, DependencyProperty property)
        {
            this.target = target;
            this.property = property;
        }

        public void Cancel(object valueToRollback)
        {
            isCancelling = true;
            target.SetValue(property, valueToRollback);
        }

        public void Finish()
        {
            isCancelling = false;
        }

        public bool IsCancelling { get { return isCancelling; } }
    }
}
